package com.micrometa.km.ui.components.tree;

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;

import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.MutableTreeNode;
import javax.swing.tree.TreePath;

import com.micrometa.km.ui.util.IconUtil;
import com.micrometa.km.util.FileUtil;

public class PopupMenu extends JPopupMenu {
	private static final long serialVersionUID = 1L;
	private JMenuItem addItem = new JMenuItem("添加",IconUtil.getIcon("/add"));
	private JMenuItem delItem = new JMenuItem("删除",IconUtil.getIcon("/delete"));
	private JMenuItem editItem = new JMenuItem("修改",IconUtil.getIcon("/edit"));
	private JMenuItem reItem = new JMenuItem("刷新",IconUtil.getIcon("/reload"));
	private JTree tree;

	
	public JMenuItem getAddItem() {
		return addItem;
	}


	public JMenuItem getDelItem() {
		return delItem;
	}


	public JMenuItem getEditItem() {
		return editItem;
	}


	public JMenuItem getReItem() {
		return reItem;
	}


	public PopupMenu(JTree tre) {
		tree=tre;
		this.add(addItem);
		this.add(delItem);
		this.add(editItem);
		this.add(reItem);

		addItem.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				// new node
				String s = JOptionPane.showInputDialog(
				// showInputDialog,showMessageDialog
						null, "", "添加文件夹", JOptionPane.INFORMATION_MESSAGE);
				if (s == null || s.equals(""))
					return;
				FileTreeNode node = new FileTreeNode(s);
				// current node
				FileTreeNode tar = (FileTreeNode) tree
						.getLastSelectedPathComponent();
				File tarf;
				if (tar == null) {
					tar = (FileTreeNode) tree.getModel().getRoot();
				}
				tarf = tar.getFile();
				String p;
				if (tarf.isDirectory())
					p = tarf.getAbsolutePath() + "/" + s;
				else
					p = tarf.getAbsolutePath().substring(0,
							tarf.getAbsolutePath().lastIndexOf("\\") + 1)
							+ s;
				File c = new File(p);
				if (!c.exists())
					c.mkdir();
				node.setFile(c);
				if (tarf.isDirectory()) {
					tar.add(node);
					// is root
//					System.out.println(tar.isRoot());
					if (!tar.isRoot())
						tree.expandPath(new TreePath(
								((DefaultTreeModel) tree.getModel())
										.getPathToRoot(tar)));
//						TreeUtil.expandAll(tree, new TreePath(
//								((DefaultTreeModel) tree.getModel())
//										.getPathToRoot(tar)), true);
				} else {
					((FileTreeNode) tar.getParent()).add(node);
					tree.expandPath(new TreePath(tar.getParent()));
//					TreeUtil.expandAll(tree, new TreePath(tar.getParent()),
//							true);
				}
				tree.updateUI();
			}
		});
		delItem.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				FileTreeNode tar = (FileTreeNode) tree
						.getLastSelectedPathComponent();
				File tarf = tar.getFile();
				// TODO: check whether direction has sub files and if true then
				// give msg
				if (FileUtil.delete(tarf)) {
					((DefaultTreeModel) tree.getModel())
							.removeNodeFromParent(tar);
					tree.updateUI();
				}
			}
		});
		reItem.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				FileTreeNode tar = (FileTreeNode) tree
						.getLastSelectedPathComponent();
				// TODO: refresh all tree
				if (tar != null) {
					// target not treeroot
					File tarf = tar.getFile();
					if(tarf.exists()){
						if (tarf.isDirectory()) {
							FileTreeNode tarn = FileTreeNodeUtil
							.getFileTreeNodes(tarf);
							((DefaultTreeModel) tree.getModel()).insertNodeInto(
									tarn, (MutableTreeNode) tar.getParent(), tar
									.getParent().getIndex(tar));
							((DefaultTreeModel) tree.getModel())
							.removeNodeFromParent(tar);
							tree.expandPath(new TreePath(
									((DefaultTreeModel) tree.getModel())
									.getPathToRoot(tarn)));
						}else if(tarf.isFile()){
							
						}
					}else{
						((DefaultTreeModel) tree.getModel())
						.removeNodeFromParent(tar);
					}
					tree.updateUI();
				}
			}
		});
		editItem.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				FileTreeNode tar = (FileTreeNode) tree
						.getLastSelectedPathComponent();
				File tarf = tar.getFile();
				String t = tarf.getName();
				if (tarf.isFile())
					t = t.substring(0, t.lastIndexOf("."));
				String s = JOptionPane.showInputDialog(null, "要修改的文件名", t);
				if (t.equals(s) || s == null || s.equals(""))
					return;
				String p = tarf.getAbsolutePath();
				if (tarf.isFile())
					s = s+ 
					tarf.getName()
					.substring(
							tarf
							.getName()
							.lastIndexOf(".")
							);
				s = p.substring(0, p.lastIndexOf("\\") + 1) + s;
				File n=new File(s);
				tarf.renameTo(n);
				if(n.exists()){
					FileTreeNode node = new FileTreeNode();
					node.setFile(new File(s));
					((FileTreeNode) tar.getParent()).add(node);
					((FileTreeNode) tar.getParent()).remove(tar);
//					if(n.isDirectory())
//						TreeUtil.expandAll(tree, new TreePath(
//								((DefaultTreeModel) tree.getModel())
//										.getPathToRoot(tar)), true);
					tree.updateUI();
				}
			}
		});
	}
}
